﻿using System.Web.UI.WebControls;
using Splendor_Server.script.logic.data;
using Splendor_Server.script.logic.player;
using Splendor_Server.script.logic.room;
using Splendor_Server.script.net;

namespace Splendor_Server.script.logic.msg
{
    public partial class MsgHandler
    {
        //离开房间
        public static void MsgLeaveBattle(ClientState client, MsgBase msgBase)
        {
            MsgLeaveBattle msg = (MsgLeaveBattle) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            room.RemovePlayer(player.GetPlayerId());
            room.Broadcast(msg);
        }

        public static void MsgSelectGemstone(ClientState client, MsgBase msgBase)
        {
            MsgSelectGemstone msg = (MsgSelectGemstone) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            room.Broadcast(msg, player.GetPlayerId());
        }

        public static void MsgSelectMine(ClientState client, MsgBase msgBase)
        {
            MsgSelectMine msg = (MsgSelectMine) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            room.Broadcast(msg, player.GetPlayerId());
        }

        public static void MsgSelectMineBack(ClientState client, MsgBase msgBase)
        {
            MsgSelectMineBack msg = (MsgSelectMineBack) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            room.Broadcast(msg, player.GetPlayerId());
        }

        public static void MsgAcquireGemstone(ClientState client, MsgBase msgBase)
        {
            MsgAcquireGemstone msg = (MsgAcquireGemstone) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            foreach (int type in msg.types)
                player.gameData.AddGemstone(type);

            string pId = player.GetPlayerId();
            msg.pId = pId;
            room.Broadcast(msg, pId);
            room.NextTurnPlayer();
        }

        public static void MsgReturnGemstone(ClientState client, MsgBase msgBase)
        {
            MsgReturnGemstone msg = (MsgReturnGemstone) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            foreach (int type in msg.types)
                player.gameData.SubGemstone(type, 1);

            string pId = player.GetPlayerId();
            msg.pId = pId;
            room.Broadcast(msg, pId);
        }

        public static void MsgPurchaseMine(ClientState client, MsgBase msgBase)
        {
            MsgPurchaseMine msg = (MsgPurchaseMine) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            player.gameData.PurchaseMine(msg.lv, msg.mId);

            string pId = player.GetPlayerId();
            msg.pId = pId;
            room.Broadcast(msg, pId);

            room.CheckLastTurn(player);
            room.NextTurnPlayer();
        }

        public static void MsgReserveMine(ClientState client, MsgBase msgBase)
        {
            MsgReserveMine msg = (MsgReserveMine) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;
            
            player.gameData.AddGemstone((int) GameData.GemstoneType.Gold);
            room.Broadcast(msg, player.GetPlayerId());
            room.NextTurnPlayer();
        }

        public static void MsgExtraCollect(ClientState client, MsgBase msgBase)
        {
            MsgExtraCollect msg = (MsgExtraCollect) msgBase;

            Player player = client.player;
            if (player == null) return;
            Room room = RoomManager.GetRoom(player.GetRoomId());
            if (room == null) return;

            player.gameData.AddExtra(msg.eId);

            string pId = player.GetPlayerId();
            msg.pId = pId;
            room.Broadcast(msg, pId);
        
            room.CheckLastTurn(player);
        }
    }
}